<template>
  <el-dialog
    title="车辆加油申请"
    ref="tableDialog"
    :visible.sync="dialogVisible"
    width="80%"
    @close="close"
  >
    <el-form :model="formData" label-width="140px" :rules="formRules" ref="form">
      <el-row>
        <el-col>
          <el-form-item label="请选择车辆" prop="carid">
            <el-row>
              <el-button type="primary" size="mini" @click="openCarListDialog">选择车辆</el-button>
            </el-row>
            <el-table :data="tableData" border style="width: 100%" background>
              <el-table-column fixed type="index" label="序号" align="center" width="60"></el-table-column>
              <el-table-column prop="carCode" label="车牌号" align="center"></el-table-column>
              <el-table-column prop="useType" label="车辆使用性质" align="center"></el-table-column>
              <el-table-column prop="pinpai" label="品牌" align="center"></el-table-column>
            </el-table>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col>
          <el-form-item label="费用">
            默认为加油类型
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer" style="text-align: left;">
      <el-button type="primary" @click="save">保存数据</el-button>
      <el-button @click="resetForm">重置表单</el-button>
    </span>
    <carlist ref="carlistComp"></carlist>
  </el-dialog>
</template>

<script>
import { apply } from "@/api/carju";
import { mapGetters } from "vuex";
import carlist from "./carlist";
export default {
  components: { carlist },
  data: function() {
    return {
      dialogVisible: false,
      types: [
        { name: "维修", id: 0 },
        { name: "保养", id: 1 },
        { name: "维修保养", id: 2 }
      ],
      formData: {
        type: 0,
        fileurl1: null,
        filename1: null,
        price: null,
        reason: null,
        shrid: null,
        carid: null
      },
      tableData: [], //选择的车辆
      sprListData: [], //审核人列表
      webUploadUrl: "", //文件上传地址
      //表单验证
      formRules: {
        reason: [
          {
            required: true,
            message: "请输入申请原因",
            trigger: "blur"
          }
        ],
        price: [
          {
            required: true,
            message: "请输入费用",
            trigger: "blur"
          }
        ],
        type: [
          {
            required: true,
            message: "请选择",
            trigger: "blur"
          }
        ],
        shrid: [
          {
            required: true,
            message: "请选择",
            trigger: "blur"
          }
        ],
        carid: [
          {
            required: true,
            message: "请选择",
            trigger: "blur"
          }
        ],
        fileurl1: [
          {
            required: true,
            message: "请上传",
            trigger: "blur"
          }
        ]
      }
    };
  },
  computed: {
    ...mapGetters(["userInfo", "token"])
  },
  created() {
    this.webUploadUrl = this.VUE_APP_BASE_API + "/common/webupload.shtml";
  },
  methods: {
    open() {
      this.dialogVisible = true;
      this.sprListJson();
    },
    //打开车辆选择页面
    openCarListDialog() {
      this.$refs.carlistComp.open();
    },
    //子组件完成车辆选择的回调
    onCarSelect(carData) {
      this.tableData.splice(0, this.tableData.length, carData);
      this.formData.carid = carData.id;
    },
    resetForm() {
      this.$refs.form.resetFields();
    },
    //获取审批人列表
    async sprListJson() {
      let r = await sprListJson();
      this.sprListData.splice(0, this.sprListData.length, ...r);
    },
    save() {
      this.$refs.form.validate(async pass => {
        if (!pass) {
          return;
        }
        try {
          let r = await apply({carid:this.formData.carid});
          if (r && r.error == 0) {
            this.$message({
              message: "提交成功",
              type: "success"
            });
            this.close();
          } else {
            this.$message({
              message: "出错了",
              type: "error"
            });
          }
        } catch (error) {
          this.$message({
            message: "出错了",
            type: "error"
          });
        }
      });
    },
    close() {
      this.dialogVisible = false;
      this.$parent.fetchData();
    }
  }
};
</script>

<style lang="scss" scoped>
.s_title {
  background: #f6f6f6;
  height: 40px;
  line-height: 40px;
  padding-left: 10px;
  margin-top: 0;
}
</style>